package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.warning.domain.entity.DrawingBoardDevicexy;
import com.ruoyi.warning.domain.vo.DrawingBoardDevicexyVO;
import com.ruoyi.warning.domain.vo.LinkVO;
import com.ruoyi.warning.mapper.DrawingBoardDevicexyMapper;
import com.ruoyi.warning.mapper.LinksMapper;
import com.ruoyi.warning.service.IDrawingBoardDevicexyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-04-27
 */
@Service
public class DrawingBoardDevicexyServiceImpl extends ServiceImpl<DrawingBoardDevicexyMapper, DrawingBoardDevicexy> implements IDrawingBoardDevicexyService {
    @Resource
    private LinksMapper linksMapper;
    @Resource
    private DrawingBoardDevicexyMapper drawingBoardDevicexyMapper;
    @Override
    public Object resetXY(List<Integer> list) {
        LambdaUpdateWrapper<DrawingBoardDevicexy> wrapper = new UpdateWrapper<DrawingBoardDevicexy>().lambda()
                .in(DrawingBoardDevicexy::getId,list).set(DrawingBoardDevicexy::getX,null).set(DrawingBoardDevicexy::getY,null);
        return this.update(wrapper);
    }

    @Override
    public Object getDrawingBoardTopology(Integer id) {
        List<DrawingBoardDevicexyVO> devicexyList= drawingBoardDevicexyMapper.getList(id);
        if(devicexyList.size()==0){
            return null;
        }
        // 找links
        List<LinkVO> links = linksMapper.getLinkVOList(null);
        List<LinkVO> linksM = linksMapper.getLinkVOManualList(null);
        List<LinkVO> linksC = linksMapper.getLinkVOManualCloudList(null);
        Map<String, LinkVO> linkMap = new LinkedHashMap<>();
        //查重
        for (LinkVO linkVO : links) {
            linkMap.put(linkVO.getLocalPortId()+"_"+linkVO.getRemotePortId(), linkVO);
        }
        for (LinkVO linkVO : linksM) {
            linkMap.put(linkVO.getLocalPortId()+"_"+linkVO.getRemotePortId(), linkVO);
        }
        List<LinkVO> linkList = new ArrayList<>(linkMap.values());
//        links.addAll(linksM);
        linkList.addAll(linksC);
        Map<String, Object> result = new HashMap<>();
        result.put("links", linkList);
        result.put("devices", devicexyList);
        return result;
    }

}
